package com.ruoyi.blindBoxManage.controller;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.blindBoxManage.domain.BlindBoxPublicProduct;
import com.ruoyi.blindBoxManage.mapper.ClassMapper;
import com.ruoyi.blindBoxManage.service.IBlindBoxPublicProductService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 盲盒公共商品Controller
 *
 * @author ruoyi
 * @date 2025-09-05
 */
@RestController
@RequestMapping("/blindBoxPublicProduct/blindBoxPublicProduct")
public class BlindBoxPublicProductController extends BaseController {
    @Autowired
    private IBlindBoxPublicProductService blindBoxPublicProductService;
    @Resource
    private ClassMapper classMapper;
    /**
     * 查询盲盒公共商品列表
     */
    @PreAuthorize("@ss.hasPermi('blindBoxPublicProduct:blindBoxPublicProduct:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlindBoxPublicProduct blindBoxPublicProduct) {
        if (!StrUtil.isEmpty(blindBoxPublicProduct.getClassName())) {
            Integer classId = classMapper.selectIdByName(blindBoxPublicProduct.getClassName());
            if (classId != null) {
                blindBoxPublicProduct.setClassId(classId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        startPage();
        List<BlindBoxPublicProduct> list = blindBoxPublicProductService.selectBlindBoxPublicProductList(blindBoxPublicProduct);
        if (!CollectionUtils.isEmpty(list)) {
            for (BlindBoxPublicProduct boxProduct : list) {
                String className = classMapper.selectNameById(boxProduct.getClassId());
                boxProduct.setClassName(!StrUtil.isEmpty(className) ? className : "");
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出盲盒公共商品列表
     */
    @PreAuthorize("@ss.hasPermi('blindBoxPublicProduct:blindBoxPublicProduct:export')")
    @Log(title = "盲盒公共商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlindBoxPublicProduct blindBoxPublicProduct) {
        List<BlindBoxPublicProduct> list = blindBoxPublicProductService.selectBlindBoxPublicProductList(blindBoxPublicProduct);
        if (!CollectionUtils.isEmpty(list)) {
            for (BlindBoxPublicProduct boxProduct : list) {
                String className = classMapper.selectNameById(boxProduct.getClassId());
                boxProduct.setClassName(!StrUtil.isEmpty(className) ? className : "");
            }
        }
        ExcelUtil<BlindBoxPublicProduct> util = new ExcelUtil<BlindBoxPublicProduct>(BlindBoxPublicProduct.class);
        util.exportExcel(response, list, "盲盒公共商品数据");
    }

    /**
     * 获取盲盒公共商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('blindBoxPublicProduct:blindBoxPublicProduct:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        BlindBoxPublicProduct blindBoxPublicProduct = blindBoxPublicProductService.selectBlindBoxPublicProductById(id);
        if (!ObjectUtils.isEmpty(blindBoxPublicProduct)) {
                String className = classMapper.selectNameById(blindBoxPublicProduct.getClassId());
            blindBoxPublicProduct.setClassName(!StrUtil.isEmpty(className) ? className : "");

        }
        return success(blindBoxPublicProduct);
    }

    /**
     * 新增盲盒公共商品
     */
    @PreAuthorize("@ss.hasPermi('blindBoxPublicProduct:blindBoxPublicProduct:add')")
    @Log(title = "盲盒公共商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlindBoxPublicProduct blindBoxPublicProduct) {
        return toAjax(blindBoxPublicProductService.insertBlindBoxPublicProduct(blindBoxPublicProduct));
    }

    /**
     * 修改盲盒公共商品
     */
    @PreAuthorize("@ss.hasPermi('blindBoxPublicProduct:blindBoxPublicProduct:edit')")
    @Log(title = "盲盒公共商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlindBoxPublicProduct blindBoxPublicProduct) {
        return toAjax(blindBoxPublicProductService.updateBlindBoxPublicProduct(blindBoxPublicProduct));
    }

    /**
     * 删除盲盒公共商品
     */
    @PreAuthorize("@ss.hasPermi('blindBoxPublicProduct:blindBoxPublicProduct:remove')")
    @Log(title = "盲盒公共商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(blindBoxPublicProductService.deleteBlindBoxPublicProductByIds(ids));
    }
}
